package com.shruijin.wy.thread.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

@Slf4j
public class SemaphoreExample {

    private  static final int totalCount=200;

    public static void main(String[] args) throws InterruptedException {
        ExecutorService exec = Executors.newCachedThreadPool();
        final Semaphore semaphore = new Semaphore(20);
        for (int i=0;i<totalCount;i++){
            final  int threadNum  = i;
            exec.execute(()->{
                try{
                    semaphore.acquire();
                    test3(threadNum);
                    semaphore.release();
                } catch (Exception e){
                    log.error("exception {}",e);
                }
            });
        }
        log.info("finished!");
        exec.shutdown();
    }

    private  static void test3(int threadNum) throws Exception{
        log.info("thread number is :{}",threadNum);
        Thread.sleep(1000);
    }
}
